package com.jjc.android.dialog

import android.view.View
import com.github.gzuliyujiang.wheelview.contract.TextProvider
import com.jjc.android.R
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.enums.PopupPosition

class AttachListDialog<T>(private val attachView: View) {

    lateinit var list:List<T>
    var imageUrls:List<String>? = null
    var onItemClick:((position:Int,value:T)->Unit)?=null
    var offsetX = 0

    var popupPosition:PopupPosition = PopupPosition.Left

    fun contentList(list:List<T>):AttachListDialog<T>{
        this.list = list
        return this
    }

    fun imageUrls(image:List<String>?):AttachListDialog<T>{
        imageUrls = image
        return this
    }

    fun offsetX(offsetX:Int):AttachListDialog<T>{
        this.offsetX = offsetX
        return this
    }

    fun popupPosition(popupPosition:PopupPosition):AttachListDialog<T>{
        this.popupPosition = popupPosition
        return this
    }

    fun onItemClick(onItemClick:(position:Int,value:T)->Unit):AttachListDialog<T>{
        this.onItemClick = onItemClick
        return this
    }

    fun build(): BasePopupView{
        return XPopup.Builder(attachView.context)
            .hasShadowBg(false)
            .atView(attachView)
            .isLightStatusBar(true)
            .popupPosition(popupPosition)
            .asCustom(CustomAttachListPopupView(attachView.context).apply {
                bindLayout(R.layout.dialog_attach_pop)
                setStringDataUrl(list.map { if(it is TextProvider) it.provideText() else it.toString() },imageUrls)
                setOnSelectListener{position, text ->
                    onItemClick?.invoke(position, list[position])
                }
            })
    }


}